package com.bsj.pay.controller.merchant;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bsj.pay.model.MerchantBean;
import com.bsj.pay.model.OrderBean;
import com.bsj.pay.service.MerchantService;
import com.bsj.pay.service.OrderService;
import com.bsj.pay.util.date.DateTimeUtil;

@RequestMapping("/merchant")
@Controller("merchantIndexController")
public class IndexController {
	
	@Autowired
	private MerchantService merchantService;
	
	@Autowired
	private OrderService orderService;
	
	@RequestMapping("")
    public ModelAndView index(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("merchant/index");
		return mav;
    }
	
	@RequestMapping("/index.html")
    public ModelAndView main(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("merchant/index");
		return mav;
    }
	
	@RequestMapping("/welcome.html")
    public ModelAndView welcome(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		MerchantBean muser=(MerchantBean) request.getSession().getAttribute("muser");
		muser=merchantService.getById(muser.getId());
		OrderBean queryModel=new OrderBean();
		queryModel.setMerchant_id(muser.getId());
		mav.addObject("allOrderCount", orderService.count(queryModel));
		queryModel.setStatus(1);
		mav.addObject("allOrderSum", new BigDecimal(orderService.sumMoney(queryModel)).toString());
		
		queryModel.setStatus(null);
		queryModel.setCreate_time(DateTimeUtil.getTodayUnixTime());
		mav.addObject("todayOrderCount", orderService.count(queryModel));
		
		queryModel.setStatus(1);
		mav.addObject("todayOrderSum", new BigDecimal(orderService.sumMoney(queryModel)).toString());
		queryModel.setPay_type("wechat");
		mav.addObject("todayOrderSumWx", new BigDecimal(orderService.sumMoney(queryModel)).toString());
		queryModel.setPay_type("alipay");
		mav.addObject("todayOrderSumAli", new BigDecimal(orderService.sumMoney(queryModel)).toString());
		
		OrderBean orderBean = new OrderBean();
        orderBean.setStatus(1);
        orderBean.setMerchant_id(muser.getId());
        orderBean.setPay_type("alipay");
        List<OrderBean> alipayOrders = this.orderService.queryDayList(orderBean);
        orderBean.setPay_type("wechat");
        List<OrderBean> wxOrders = this.orderService.queryDayList(orderBean);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        String alipayStr = "";
        String wxStr = "";
        String dateStr = "";
        for (Integer i = -6; i < 1; ++i) {
            calendar.setTime(date);
            calendar.add(5, i);
            String cDate = df.format(calendar.getTime());
            Long sum = 0L;
            Long wxSum = 0L;
            for (OrderBean order : alipayOrders) {
                if (cDate.equals(order.getTime())) {
                    sum = order.getAll_amount();
                    break;
                }
            }
            for (OrderBean order : wxOrders) {
                if (cDate.equals(order.getTime())) {
                    wxSum = order.getAll_amount();
                    break;
                }
            }
            alipayStr = alipayStr + "," + sum;
            wxStr = wxStr + "," + wxSum;
            dateStr = dateStr + "," + cDate;
        }
        mav.addObject("alipayStr", alipayStr.substring(1));
        mav.addObject("wxStr", wxStr.substring(1));
        mav.addObject("dateStr", dateStr.substring(1));
		mav.addObject("currentDateTime", DateTimeUtil.getCurrentDateTimeStr());
		mav.setViewName("merchant/welcome");
		return mav;
    }
	

	@RequestMapping("/loginPage.html")
    public ModelAndView loginPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("merchant/login");
		return mav;
    }
	
	@RequestMapping("/docPage.html")
    public ModelAndView docPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("merchant/doc");
		return mav;
    }
	
	@RequestMapping("/updatePasswordPage.html")
    public ModelAndView updatePasswordPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("merchant/password");
		return mav;
    }
	
	@RequestMapping("/loginOut.html")
    public ModelAndView loginOut(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		request.getSession().removeAttribute("muser");
		mav.setViewName("merchant/login");
		return mav;
    }
	
}
